package cn.xzqwjw.taskmanager.controller.api;

import cn.xzqwjw.taskmanager.service.SysRolePurviewService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;

/**
 * @author rush
 */
@RestController
@RequestMapping("/api/role-purview")
@Validated
public class SysRolePurviewController {

  private final SysRolePurviewService sysRolePurviewService;

  @Autowired
  public SysRolePurviewController(SysRolePurviewService sysRolePurviewService) {
    this.sysRolePurviewService = sysRolePurviewService;
  }

  /**
   * 根据权限id查询得到所有拥有此权限的角色id列表
   */
  @GetMapping("/purview")
  public String getRoleIdsByPurviewId(
      @NotNull(message = "id不能为空")
      @Min(value = 1, message = "id必须大于0")
      @RequestParam(value = "PurviewId") Integer idPurview) {
    return sysRolePurviewService.getRoleIdsByPurviewId(idPurview);
  }

  /**
   * 根据角色id查询得到此角色拥有的所有权限id列表
   */
  @GetMapping("/role")
  public String getPurviewIdsByRoleId(
      @NotNull(message = "id不能为空")
      @Min(value = 1, message = "id必须大于0")
      @RequestParam(value = "RoleId") Integer idRole) {
    return sysRolePurviewService.getPurviewIdsByRoleId(idRole);
  }

}
